$(window).ready(function () {
    $(".btn1").click(function () {
        window.open("downfile.php");
    });
    $(".btn2").click(function () {
        var type = "png";
        var canvasdiv = "<canvas width=795 height=1700 style='display:none'></canvas>";
        html2canvas($(".bg"), {
            onrendered: function (canvas) {
                $(".bg").append(canvasdiv);
                var imgData = canvas.toDataURL();
                var _fixType = function (type) {
                    type = type.toLowerCase().replace(/jpg/i, 'jpg');
                    var r = type.match(/jpeg|jpg|png|bmp|gif/)[0];
                    return 'image/' + r;
                };

// 加工image data，替换mime type
                imgData = imgData.replace(_fixType(type), 'image/octet-stream');
                /**
                 * 在本地进行文件保存
                 * @param  {String} data     要保存到本地的图片数据
                 * @param  {String} filename 文件名
                 */
                var saveFile = function (data, filename) {
                    var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                    save_link.href = data;
                    save_link.download = filename;

                    var event = document.createEvent('MouseEvents');
                    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                    save_link.dispatchEvent(event);
                };

// 下载后的问题名
                var filename = 'jianli_' + (new Date()).getTime() + '.' + type;
// download
                saveFile(imgData, filename);
            }
        });
    });
    $(".btn3").click(function () {
        var type = "png";
        var canvasdiv = "<canvas width=794 height=1200 style='display:none'></canvas>";
        html2canvas($(".bg"), {
            onrendered: function (canvas) {
                $(".bg").append(canvasdiv);
                var imgData = canvas.toDataURL();
                var _fixType = function (type) {
                    type = type.toLowerCase().replace(/jpg/i, 'jpg');
                    var r = type.match(/jpeg|jpg|png|bmp|gif/)[0];
                    return 'image/' + r;
                };

// 加工image data，替换mime type
                imgData = imgData.replace(_fixType(type), 'image/octet-stream');
                /**
                 * 在本地进行文件保存
                 * @param  {String} data     要保存到本地的图片数据
                 * @param  {String} filename 文件名
                 */
                var saveFile = function (data, filename) {
                    var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                    save_link.href = data;
                    save_link.download = filename;

                    var event = document.createEvent('MouseEvents');
                    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                    save_link.dispatchEvent(event);
                };

// 下载后的问题名
                var filename = 'jianli_' + (new Date()).getTime() + '.' + type;
// download
                saveFile(imgData, filename);
            }
        });
    });
    $(".btn4").click(function () {
        window.open("downfile2.php");
    });
});